﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    public class CollisionManager
    {
        public void Update(BulletManager bulletManager, AlienManager alienManager)
        {
            foreach (Alien alien in alienManager.aliens)
            {
                if (alien.collision == false)
                    continue;

                Rectangle alienRect = alien.ComputeRect();
                foreach (Bullet bullet in bulletManager.bullets)
                {
                    if (alienRect.Intersects(bullet.ComputeRect()))
                    {
                        alien.OnEventHit(bullet);
                        bullet.live = false;
                    }
                    
//                     float r2 = bullet.radius * bullet.radius;
//                     float d2 = Vector2.DistanceSquared(bullet.pos, alien.pos);
//                     if (d2 < r2)
//                     {
//                     }
                }

                foreach (Bullet b in bulletManager.freezeBullets)
                {
                    Rectangle bulletRect = b.ComputeRect();

                    if (alienRect.Intersects(bulletRect))
                    {
                        alien.OnEventFreeze();
                    }
                }

                foreach (Lazor lazor in bulletManager.lazors)
                {
                    bool inter = lazor.ComputeRect().Intersects(alienRect);
                    if (inter)
                    {
                        if (lazor.alive)
                        {
                            alien.OnEventHitByLazor();
                            if (alien.IsAlive() == true)
                            {
                                lazor.top = alienRect.Bottom;
                            }
                        }
                        else
                        {
                            lazor.top = alienRect.Bottom;
                        }
                    }
                }
            }
        }

        public void UpdatePlayerCollision(BulletManager alienBulletManager, AlienManager alienManager, Plane plane)
        {
            foreach (Bullet bullet in alienBulletManager.bullets)
            {
                float r2 = 100;
                float d2 = Vector2.DistanceSquared(bullet.pos, plane.pos);
                if (d2 < r2)
                {
                    plane.OnEventHit();
                }
            }

            foreach (Alien alien in alienManager.aliens)
            {
                Rectangle alienRect = alien.ComputeRect();
                Rectangle planeRect = plane.ComputeRect();
                if (alienRect.Intersects(planeRect))
                {
                  
                    {
                        plane.OnEventHit();
                    }
                }
            }
        }
    }
}
